package com.xuecheng.media.service.impl;

import com.xuecheng.media.mapper.MediaFilesMapper;
import com.xuecheng.media.mapper.MediaProcessHistoryMapper;
import com.xuecheng.media.mapper.MediaProcessMapper;
import com.xuecheng.media.model.po.MediaFiles;
import com.xuecheng.media.model.po.MediaProcess;
import com.xuecheng.media.model.po.MediaProcessHistory;
import com.xuecheng.media.service.MediaFileProcessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: Hes
 * @description 媒资文件处理业务方法实现
 * @date: 2023/6/22 21:06
 */
@Slf4j
@Service
public class MediaFileProcessServiceImpl implements MediaFileProcessService {

    @Autowired
    MediaProcessMapper mediaProcessMapper;

    @Autowired
    MediaFilesMapper mediaFilesMapper;

    @Autowired
    MediaProcessHistoryMapper mediaProcessHistoryMapper;

    /**
     * 查询待处理任务
     *
     * @param shardIndex
     * @param shardTotal
     * @param count
     * @return
     */
    @Override
    public List<MediaProcess> getMediaProcessList(int shardIndex, int shardTotal, int count) {
        List<MediaProcess> mediaProcesses = mediaProcessMapper.selectListByShardIndex(shardIndex, shardTotal, count);
        System.out.println(mediaProcesses);
        return mediaProcesses;
    }

    /**
     * 开启任务，根据id更新任务状态为正在处理
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public boolean startTask(Long id) {
        int i = mediaProcessMapper.startTask(id);

        return i <= 0 ? false : true;
    }

    /**
     * 任务处理完成需要更新任务处理结果，
     * 任务执行成功更新视频的URL、及任务处理结果，
     * 将待处理任务记录删除，
     * 同时向历史任务表添加记录。
     *
     * @param taskId
     * @param status
     * @param fileId
     * @param url
     * @param errorMsg
     */
    @Override
    @Transactional
    public void saveProcessFinishStatus(Long taskId, String status, String fileId, String url, String errorMsg) {
        MediaProcess mediaProcess = mediaProcessMapper.selectById(taskId);
        //如果任务记录表没有记录直接结束
        if (mediaProcess == null) {
            return;
        }
        //任务没有处理成功
        if ("3".equals(status)) {
            int failCount = mediaProcess.getFailCount();
            mediaProcess.setStatus(status);
            mediaProcess.setErrormsg(errorMsg);
            mediaProcess.setFileId(fileId);
            mediaProcess.setFailCount(failCount + 1);
            int i = mediaProcessMapper.updateById(mediaProcess);
            if (i <= 0) {
                log.debug("任务跟新失败,taskId：{},errorMsg:{}", taskId, errorMsg);
                return;
            }
        }
        //视频处理成功，更新status url fileId
        mediaProcess.setStatus("2");
        mediaProcess.setUrl(url);
        mediaProcess.setFinishDate(LocalDateTime.now());
        mediaProcessMapper.updateById(mediaProcess);
        //更新mediaProcess后，更新meidafiles的url
        MediaFiles mediaFiles = mediaFilesMapper.selectById(fileId);
        if (mediaFiles != null) {
            mediaFiles.setUrl(url);
            int updateMediaFiles = mediaFilesMapper.updateById(mediaFiles);
            if (updateMediaFiles <= 0) {
                log.debug("更新mediafiles的url失败,fileId：{},url:{}", fileId, url);
                return;
            }
        }
        //添加视频任务历史表
        MediaProcessHistory mediaProcessHistory = new MediaProcessHistory();
        BeanUtils.copyProperties(mediaProcess, mediaProcessHistory);
        int insertMediaProcessHistory = mediaProcessHistoryMapper.insert(mediaProcessHistory);
        if (insertMediaProcessHistory <= 0) {
            log.debug("添加mediaProcessHistory表失败,taskId：{},fileId：{},url:{}", taskId, fileId, url);
            return;
        }
        //删除视频任务
        int delByMediaProcess = mediaProcessMapper.deleteById(mediaProcess);
        if (delByMediaProcess <= 0) {
            log.debug("删除mediaProcess的url失败,fileId：{},url:{}", fileId, url);
            return;
        }

    }
}
